package com.sservice.web.controller.admin;

import com.sservice.common.SameUrlData;
import com.sservice.model.vo.SearchRecordVo;
import com.sservice.service.service.ISearchRecordService;
import com.sservice.base.entity.dto.BaseResult;
import com.sservice.base.entity.dto.ListResult;
import com.sservice.base.entity.dto.Pagination;
import com.sservice.model.entity.SearchRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 *  查询记录
 */
@Controller
@RequestMapping("/admin/searchRecord")
public class SearchRecordController {

    @Autowired
    private ISearchRecordService service;

    /**
     * 创建
     *
     * @return
     */
    @ResponseBody
    @SameUrlData
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public BaseResult<Integer> create(SearchRecord arg, HttpSession session) {
        int service_res = service.insertSelective(arg);
        BaseResult<Integer> res = new BaseResult<Integer>(true, service_res);
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public BaseResult<SearchRecord> get(SearchRecord arg, HttpSession session) {
        SearchRecord service_res = service.selectByPrimaryKey(arg.getId());
        BaseResult<SearchRecord> res = new BaseResult<SearchRecord>(true, service_res);
        return res;
    }

    /**
     * @param arg
     * @return
     */
    @ResponseBody
    @SameUrlData
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public BaseResult<Object> delete(SearchRecord arg) {
        BaseResult<Object> res = null;
        int service_res = service.deleteByPrimaryKey(arg.getId());
        res = new BaseResult(true, service_res);
        return res;
    }

    @ResponseBody
    @SameUrlData
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult<Object> update(SearchRecord arg) {
        BaseResult<Object> res = null;
        int service_res = service.updateByPrimaryKeySelective(arg);
        res = new BaseResult(true, service_res);
        return res;
    }

    /***
     * 分页查询
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ListResult query(SearchRecordVo arg) {
        List<SearchRecord> list = service.queryByPage(arg);
        int total = service.queryCount(arg);
        ListResult res = new ListResult(true, list);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        res.setPage(page);
        return res;
    }
}
